package cn.learn.async;

import cn.learn.async.service.AsyncService1;
import cn.learn.async.service.AsyncService2;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author huangyezhan
 * @version 1.0
 * @date 2020年12月25日21:40
 */
@RestController
public class AsyncController2 {
    @Resource
    private AsyncService1 asyncService1;
    @Resource
    private AsyncService2 asyncService2;

    @RequestMapping(value = "test/async",method = RequestMethod.GET)
    public Long testAsync() throws ExecutionException, InterruptedException {
        long start = System.currentTimeMillis();

        Future<String> future1 = asyncService1.testAsync1("呼叫异步，我是A");

        Future<String> testAsync2 = asyncService1.testAsync2("我是A中的B，间谍");

        Future<String> future2 = asyncService2.testAsync1("我是B，我耗时要5s");

        String s = future1.get();
        String s1 = future2.get();
        String s2 = testAsync2.get();

        System.out.println(s + s1 + s2);

        return System.currentTimeMillis() - start;
    }

}
